#!/bin/bash

CWD=$(dirname $(readlink -f $0))

NAME=fluxbox
VERSION=1.3.5

# first install stock version

apt update
apt install --no-install-recommends --yes \
    $NAME \
    feh \
    compton \
    volumeicon-alsa \
    murrine-themes \
    libnotify4 \
    libgtk2.0-0 \
    xfce4-panel \
    scrot

. ../../cleanup

(cd rootcopy && cp --parents -afr * /)
chown guest:users /home/guest
chown -R guest:users /home/guest/.gtkrc-2.0
chown -R guest:users /home/guest/.config

savechanges /$NAME-stock.sb

cd /
mkdir /squashfs-root
mount -t tmpfs tmpfs /squashfs-root
unsquashfs -f /$NAME-stock.sb

# create user directories
for dir in Desktop Documents Downloads Music Pictures Public Templates Videos; do
   mkdir -p /squashfs-root/home/guest/$dir
   mkdir -p /squashfs-root/root/$dir
done
chown -R guest:users /squashfs-root/home/guest

# then, we want to patch it to implement window resizing!
# so we will recompile whole package

REBUILD=/tmp/$NAME-rebuild-$$

apt update
apt --yes build-dep $NAME

mkdir -p $REBUILD
cd $REBUILD

apt source $NAME

cd $NAME-$VERSION

cat $CWD/patches/fluxbox-slax.diff > debian/patches/fluxbox-slax.diff
echo fluxbox-slax.diff >> debian/patches/series

# enable imlib2 support
cat $CWD/patches/imlib2-config >/usr/bin/imlib2-config
chmod ugo+x /usr/bin/imlib2-config

dpkg-buildpackage -us -uc

rm -Rf debian/$NAME/DEBIAN
rm -Rf debian/$NAME/usr/share/doc
rm -Rf debian/$NAME/usr/share/images

cp debian/$NAME/usr/bin/fluxbox /squashfs-root/usr/bin


# add xlunch from sources
wget -O /tmp/xlunch.tar.gz https://github.com/Tomas-M/xlunch/archive/v4.7.5.tar.gz
cd /tmp
tar -xf xlunch.tar.gz
cd xlunch-4.7.5
make
cp xlunch /squashfs-root/usr/bin
cp extra/gentriesquick /squashfs-root/usr/bin/xlunch_genquick
mkdir -p /squashfs-root/usr/share/icons/hicolor/48x48/apps/
cp extra/ghost.png /squashfs-root/usr/share/icons/hicolor/48x48/apps/xlunch_ghost.png

# add tray icon launcher
wget -O /tmp/systrayiconlauncher.tar.gz https://github.com/Tomas-M/SystrayIconLauncher/archive/master.tar.gz
cd /tmp
tar -xf systrayiconlauncher.tar.gz
cd SystrayIconLauncher-master
apt install --yes libgtk2.0-dev
make
cp systrayicon /squashfs-root/usr/bin


# add gtkdialog
wget -O /tmp/gtkdialog.tar.gz https://github.com/Tomas-M/GTKdialog/archive/master.tar.gz
cd /tmp
tar -xf gtkdialog.tar.gz
cd GTKdialog-master
make
cp gtkdialog /squashfs-root/usr/bin


# battery icon app
cd /tmp
wget https://github.com/valr/cbatticon/archive/1.6.10.tar.gz
tar -xf 1.6.10.tar.gz
cd cbatticon-1.6.10
cp -R /usr/include/glib-2.0/* /usr/include
cp -R /usr/include/gtk-2.0/* /usr/include
apt install --yes libnotify-dev
sed -i "s/WITH_GTK3 = 1/WITH_GTK3 = 0/" Makefile
CFLAGS="-O2 \
-I/usr/lib/x86_64-linux-gnu/glib-2.0/include/ \
-I/usr/lib/x86_64-linux-gnu/gtk-2.0/include/ \
-I/usr/include/gdk-pixbuf-2.0/ \
-I/usr/include/atk-1.0/ \
-I/usr/include/cairo \
-I/usr/include/pango-1.0 \
-I/usr/include/glib-2.0 \
-I/usr/include/gtk-2.0" \
make
cp cbatticon /squashfs-root/usr/bin

dir2sb /squashfs-root /03-desktop.sb
